package com.canbot.u05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.KqGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 考勤库选择xuAdapter
 */

public class KqGroupAdapter extends BaseAdapter {

        private Context context;

        private List<KqGroup> data;

        private LayoutInflater inflater;

        public KqGroupAdapter(Context context, List<KqGroup> data) {
                this.context = context;
                setData(data);
                inflater = LayoutInflater.from(context);

        }

        private void setData(List<KqGroup> data) {
                if (data == null) {
                        data = new ArrayList<>();
                }
                this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
                ViewHolder holder = null;
                if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = inflater.inflate(R.layout.layout_kq_choose_group, null);
                        holder.fileName = (TextView) convertView.findViewById(R.id.item_tv_group);
                        holder.checkBox = (CheckBox) convertView.findViewById(R.id.iv_choose);

                        convertView.setTag(holder);
                }
                else {
                        holder = (ViewHolder) convertView.getTag();
                }
                KqGroup kqGroup = data.get(position);
                holder.fileName.setText(kqGroup.getGroupName());
                if (kqGroup.isChecked()) {
                        holder.checkBox.setVisibility(View.VISIBLE);
                }
                else {
                        holder.checkBox.setVisibility(View.INVISIBLE);
                }

                return convertView;
        }

        @Override
        public int getCount() {
                return data.size();
        }

        @Override
        public Object getItem(int i) {
                return data.get(i);
        }

        @Override
        public long getItemId(int i) {
                return i;
        }

        private class ViewHolder {

                private TextView fileName;

                private CheckBox checkBox;

        }

        /**
         * 更新控件显示
         *
         * @param posi
         * @param gridView
         */
        public void updataView(int posi, GridView gridView) {
                int visibleFirstPosi = gridView.getFirstVisiblePosition();
                int visibleLastPosi = gridView.getLastVisiblePosition();

                if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {
                        View view = gridView.getChildAt(posi - visibleFirstPosi);
                        ViewHolder holder = (ViewHolder) view.getTag();
//            holder.checkBox.setChecked(data.get(posi).isChecked());
                        if (data.get(posi).isChecked()) {
                                holder.checkBox.setVisibility(View.VISIBLE);
                        }
                        else {
                                holder.checkBox.setVisibility(View.INVISIBLE);
                        }
                }
                else {

                }
        }
}
